﻿using System;
using System.Collections.Generic; 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; 
using System.IO; 
using GetIMEIWeb;
 
    public partial class Recevier : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.InputStream.Length != 0)
            {
                StreamReader reader = new StreamReader(Request.InputStream, System.Text.Encoding.UTF8);
                string r = reader.ReadToEnd();
                try
                {
                    OperateType type = JsonHelper.JsonDeserialize<OperateType>(r);
                    if (type.operateId == 2)
                    {
                        String[] ss = type.contents.Split('%');
                        DateTime time = DateTime.Now;
                        String ds = time.ToString("yyyy-MM-dd HH:mm:ss");
                        String writeS = ds + "\t\t\t" + ss[1] + "\t" + ss[0];
                        saveLog(writeS);
                        FileHelper he = new FileHelper();
                        r = "1%" + he.readUrl();
                    }
                    if (type.operateId == 1)
                    {
                        saveErrorLog(type.contents);
                    }
                }
                catch (Exception ex)
                {
                    r = "0%" + ex.Message;
                }
                Response.Clear();
                Response.Write(r);
                Response.End();
            }
        }
        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="logs"></param> 
        private void saveLog(String str)
        {
            String path = AppDomain.CurrentDomain.BaseDirectory + "Log\\";
            FileHelper.CheckDirectory(path);
            DateTime time = DateTime.Now;

            String name = path + time.ToString("yyyyMMdd") + ".txt";
            FileHelper.SaveLog(name, str);
        }

        /// <summary>
        /// 保存错误信息
        /// </summary>
        /// <param name="logs"></param>
        /// <param name="storeCode"></param>
        private void saveErrorLog(String logs)
        {
            String path = AppDomain.CurrentDomain.BaseDirectory + "ErrorLog\\";
            FileHelper.CheckDirectory(path);
            DateTime time = DateTime.Now;

            String name = path + time.ToString("yyyyMMddHHmmss")+ ".txt";
            FileHelper.SaveToFile(name, logs);
        }
}